<?php
/**
 * 包装箱
 * (model)
 */
namespace App\Models\Basics;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class PackingBox extends Model
{
    protected $connection = 'mysql';

    protected $table = 'b_packing_box';

    protected $guarded = [];

    public function packing_box_area()
    {
        return $this->belongsToMany(SysArea::class,
            'b_packing_box_area',
            'packing_box_id',
            'area_id');
    }

    public static function options($where = [])
    {
        $query = $where ? self::query()->where(formatCondition($where)) : self::query();
        return $query->pluck('box_type','id');
    }

    public static function getBoxTypeById($id){
        return self::query()->where('id',$id)->value('box_type');
    }

    public static function getIdByBoxType($boxType){
        return self::query()->where('box_type',$boxType)->value('id');
    }

    //箱子关联的所有站点
    public static function getAreasById($id){
        return DB::table('b_packing_box_area')->where('packing_box_id',$id)->pluck('area_id');
    }

    //站点关联的箱子
    public static function getPackingBoxIdByAreaId($areaId){
        return DB::table('b_packing_box_area')->where('area_id',$areaId)->value('packing_box_id');
    }
}
